form 表單驗證(即時)


Posted by mijouhsieh on 2023-08-19

  1. regexp 正規表示法
  2. 使用 event (change, input, submit)

regexp 正規表示法
驗證中文 [\u4e00-\u9fa5]
驗證英文字母:[a-zA-Z]
驗證數字:[0-9]
驗證中文、英文字母和數字和 _ 底線: ^[\u4e00-\u9fa5_a-zA-Z0-9]+$
驗證first name last name: /^[a-zA-Z]+ [a-zA-Z]+$/

const regName = /^[a-zA-Z]+ [a-zA-Z]+$/;
let name = document.querySelector('#nameInput').value;
if(!regName.test(name)){
    alert('Invalid name given.');
}else{
    alert('Valid name given.');
}

How to Validate First and Last Name with Regular Expression using JavaScript


#表單 #驗證輸入資料 #驗證 #validation #form validation







Related Posts

[Preparation for Interview Basic 1] DOM Element

[Preparation for Interview Basic 1] DOM Element

npm 是什麼? yarn 是什麼?

npm 是什麼? yarn 是什麼?

HACKLAB: VULNIX Walkthrough

HACKLAB: VULNIX Walkthrough


Comments